package kt.com.bilin.coin.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import android.telephony.TelephonyManager
import com.bilin.coin.App
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kt.com.bilin.coin.common.permissionRequest
import kt.com.bilin.coin.ui.dialog.showAlertDialog
import java.lang.reflect.Method

object CplUtils {
    @SuppressLint("HardwareIds", "MissingPermission")
    fun Activity.getImei(successCallback: (imei: String) -> Unit) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            permissionRequest {
                permission(Permission.READ_PHONE_STATE)
                onGranted {
                    try {
                        val manager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                        successCallback(manager.deviceId.ifEmpty { App.OAID })
                    } catch (e: Exception) {
                        successCallback(App.OAID)
                    }
                }
                onDenied {
                    if (!AndPermission.hasPermissions(this@getImei, Permission.READ_PHONE_STATE)) {
                        showAlertDialog {
                            mCancelable = false
                            message(message = "为了更好的体验需要您授予权限")
                            positiveButton(name = "去授权") {
                                AndPermission.with(this@getImei)
                                        .runtime()
                                        .setting()
                                        .start(1)
                            }
                        }
                    }
                }
            }
        } else {
            successCallback(App.OAID)
        }
    }

    @JvmStatic
    fun Activity.getPaccXwdeviceid(successCallback: (xwdeviceid: String) -> Unit) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            permissionRequest {
                permission(Permission.READ_PHONE_STATE)
                onGranted {
                    successCallback.invoke(getIMEI(this@getPaccXwdeviceid, 0) + "," + getIMEI(this@getPaccXwdeviceid, 1) + "," + getMEID(this@getPaccXwdeviceid))
                }
                onDenied {
                    if (!AndPermission.hasPermissions(this@getPaccXwdeviceid, Permission.READ_PHONE_STATE)) {
                        showAlertDialog {
                            mCancelable = false
                            message(message = "为了更好的体验需要您授予权限")
                            positiveButton(name = "去授权") {
                                AndPermission.with(this@getPaccXwdeviceid)
                                        .runtime()
                                        .setting()
                                        .start(1)
                            }
                        }
                    }
                }
            }
        } else {
            getImei(successCallback)
        }
    }

    @JvmStatic
    fun getIMEI(context: Context, slotId: Int): String? {
        return try {
            val manager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            val method: Method = manager.javaClass.getMethod("getImei", Int::class.javaPrimitiveType)
            method.invoke(manager, slotId) as String
        } catch (e: Exception) {
            ""
        }
    }

    @JvmStatic
    fun getMEID(context: Context): String? {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        var deviceId: String?
        deviceId = try {
            val getMeid = tm.javaClass.getDeclaredMethod("getMeid")
            getMeid.invoke(tm) as String
        } catch (e: Throwable) {
            return ""
        }
        if (null == deviceId) {
            deviceId = ""
        }
        return deviceId
    }

    @JvmStatic
    @SuppressLint("MissingPermission")
    fun getDeviceId(context: Context): String? {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return try {
            tm.deviceId
        } catch (e: Exception) {
            ""
        }
    }
}